<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/19
 * Time: 8:05
 */

namespace app\index\controller;


use app\common\model\ShopApply;
use app\common\model\ShopGroup;
use app\common\model\Shop;
use app\common\utils\JsonService;

class MemberSettled extends MemberBase
{
    public function settled(){
        if($this->request->isAjax()){
            $post = $this->request->post();
            if($post['shop_group_id']){
                $shop_group = ShopGroup::getOne(['id'=>$post['shop_group_id']],'group_name');
                $post['shop_group_name'] = $shop_group['group_name'];
            }

            if($post['shop_type_id'] == 1){
                $post['shop_type_name'] = '直营店铺';
            }else{
                $post['shop_type_name'] = '入驻店铺';
            }

            $post['member_id'] = $this->member_id;

            $shop = Shop::getOne(['member_id'=>$this->member_id]);
            $shop_apply = ShopApply::getOne(['member_id'=>$this->member_id]);
            if(!empty($shop)){
                Shop::updateOne($post,['member_id'=>$this->member_id,'shop_id'=>$shop['shop_id']]);
                ShopApply::updateOne($post,['member_id'=>$this->member_id,'apply_id'=>$shop_apply['apply_id']]);
            }else{
                $res = Shop::create($post);
                $post['shop_id'] = $res->shop_id;
                ShopApply::create($post);
            }

            return JsonService::success('申请成功');
        }

        $province = getProvinceCity();
        $this->assign('province',$province);
        $shop_group = ShopGroup::getAll();
        $this->assign('shop_group',$shop_group);
        $shop = Shop::getOne(['member_id'=>$this->member_id]);
        $this->assign('shop',$shop);
        $shop_apply = ShopApply::getOne(['member_id'=>$this->member_id]);
        $this->assign('shop_apply',$shop_apply);
        return $this->fetch();
    }
}